﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using BulldogCMS.Data.Fields;

namespace BulldogCMS.Data.Templates
{
    public class XMLTemplatesProvider : ITemplatesProvider
    {
        private XDocument _xDoc = null;
        private IEnumerable<ITemplate> _templates = null;

        public IEnumerable<ITemplate> Templates
        {
            get
            {
                if (this._templates == null)
                {
                    this._templates = this.LoadTemplatesFromXDoc();
                }
                return this._templates;
            }
        }

        public XMLTemplatesProvider(XDocument xDoc)
        {
            this._xDoc = xDoc;
        }

        public ITemplate GetTemplate(string name)
        {
            return this.Templates.Where(p => p.Name == name).FirstOrDefault();
        }

        private IEnumerable<ITemplate> LoadTemplatesFromXDoc()
        {
            List<ITemplate> templates = new List<ITemplate>();
            XElement templatesEl = this._xDoc.Root.Element("templates");
            foreach (XElement templateEl in templatesEl.Elements()) {
                Template template = new Template()
                {
                    TemplatesProvider = this,
                    Name = templateEl.GetAttributeValue<string>("name"),
                    FullName = templateEl.GetAttributeValue<string>("name"),
                    AllowAllTemplates = templateEl.GetAttributeValue<bool>("allowall"),
                    BaseTemplateName = templateEl.GetAttributeValue<string>("base")
                };

                string useAttribute = templateEl.GetAttributeValue<string>("use");
                if (useAttribute != null)
                {
                    foreach (string part in useAttribute.Split(','))
                    {
                        if (part.Trim().Length > 0)
                        {
                            template.AddUseTemplateName(part);
                        }
                    }
                }

                string allowAttribute = templateEl.GetAttributeValue<string>("allow");
                if (allowAttribute != null)
                {
                    foreach (string part in allowAttribute.Split(','))
                    {
                        if (part.Trim().Length > 0)
                        {
                            template.AddAllowTemplateName(part);
                        }
                    }
                }

                if (templateEl.Element("fields") != null)
                {
                    foreach (XElement fieldEl in templateEl.Element("fields").Elements())
                    {
                        FieldDefinition fieldDefinition = new FieldDefinition()
                        {
                            Name = fieldEl.GetAttributeValue<string>("name"),
                            Type = fieldEl.GetAttributeValue<string>("type")
                        };
                        template.AddFieldDefinition(fieldDefinition);
                    }
                }

                templates.Add(template);
            }
            return templates;
        }

    }
}
